package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.constants.RConst;
import cn.itsource.mapper.DriverSummaryMapper;
import cn.itsource.pojo.domain.DriverAuthMaterial;
import cn.itsource.mapper.DriverAuthMaterialMapper;
import cn.itsource.pojo.domain.DriverSummary;
import cn.itsource.pojo.vo.DriverSummaryVo;
import cn.itsource.result.JSONResult;
import cn.itsource.service.IDriverAuthMaterialService;
import cn.itsource.utils.IdUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author yh
 * @since 2024-08-31
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {

    @Autowired
    private DriverSummaryMapper driverSummaryMapper;
    @Override
    public boolean save(DriverAuthMaterial entity) {
        int count = super.count(new LambdaQueryWrapper<DriverAuthMaterial>().eq(DriverAuthMaterial::getDriverId, entity.getDriverId())
                .ne(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_2));
        if(count==0){
            entity.setId(IdUtils.getId());
            entity.setIdcardHolding(entity.getIdcardFront());
            //设置驾驶证的反面和手持
            entity.setDrcardBack(entity.getDrcardFront());
            entity.setDrcardHolding(entity.getDrcardFront());

            entity.setRealAuthStatus(RConst.Num.INT_0);
            entity.setCreateTime(new Date());
            DriverSummary driverSummary = new DriverSummary();
            driverSummary.setId(entity.getDriverId());//用司机的id
            driverSummary.setCreateTime(new Date());
            driverSummaryMapper.insert(driverSummary);


            return super.save(entity);
        }
        return false;

    }

    @Override
    public JSONResult driverAuthMaterialByDriverId(Long driverId) {
        LambdaQueryWrapper<DriverAuthMaterial> wrapper =
                new LambdaQueryWrapper<DriverAuthMaterial>().eq(DriverAuthMaterial::getDriverId, driverId);
        DriverAuthMaterial one = super.getOne(wrapper);
        DriverAuthMaterial driver = DriverAuthMaterial.builder().
                realAuthStatus(one.getRealAuthStatus())
                .idcardAddress(one.getIdcardAddress())
                .name(one.getName())
                .birthday(one.getBirthday())
                .gender(one.getGender())
                .idcardExpire(one.getIdcardExpire())
                .carClass(one.getCarClass())
                .drcardIssueDate(one.getDrcardIssueDate())
                .drcardExpire(one.getDrcardExpire())
                .idNumber(one.getIdNumber())
                .build();
       return JSONResult.success(driver);

    }

    @Override
    public void audit(DriverAuthMaterial driverAuthMaterial) {
        driverAuthMaterial.setUpdateTime(new Date());
        driverAuthMaterial.setRealAuthStatus(1);
        driverAuthMaterial.setAuditTime(new Date());
        super.updateById(driverAuthMaterial);
    }

    @Override
    public DriverSummaryVo loadRealAuthSuccess() {
        long driveId = StpUtil.getLoginIdAsLong();
        DriverAuthMaterial one = this.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getRealAuthStatus, RConst.Num.BASE_LONG_1)
                .eq(DriverAuthMaterial::getDriverId, driveId));
        //返回状态
        DriverSummaryVo driverSummaryVo = new DriverSummaryVo();
        //返回司机的summary
        if(one!=null){
            driverSummaryVo.setRealAuthSuccess(true);
            DriverSummary driverSummary = driverSummaryMapper.selectById(driveId);
            driverSummaryVo.setTodayIncome(driverSummary.getTodayIncome());
            driverSummaryVo.setDriveDuration(driverSummary.getDriveDuration());
            driverSummaryVo.setTodayTradeOrders(driverSummary.getTodayTradeOrders());
            return driverSummaryVo;
        }else {
            driverSummaryVo.setRealAuthSuccess(false);
            return driverSummaryVo;
        }
    }
}
